package com.mycontract.suitepro.contract

import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import android.view.View
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.mycontract.suitepro.R
import com.mycontract.suitepro.base.BaseActivity
import com.mycontract.suitepro.databinding.ActivityContractSuiteContractSignSuccessBinding
import com.mycontract.suitepro.enums.PageTagEnum
import com.mycontract.suitepro.extend.*
import com.mycontract.suitepro.utils.analytics.util.AnalyticsUtil

/** 签约成功页 */
@SuppressLint("CustomSplashScreen")
class ContractSignSuccessActivity : BaseActivity() {

    //布局binding
    private lateinit var mActivityBinding: ActivityContractSuiteContractSignSuccessBinding

    //viewModel
    private val mContractViewModel by lazy {
        ViewModelProvider(this)[ContractViewModel::class.java]
    }

    override fun onContractSuiteBindingContractSuiteViewContractSuiteModel(): List<ViewModel> {
        val list = mutableListOf<ViewModel>()
        list.add(mContractViewModel)
        return list
    }

    override fun onContractSuiteBindingContractSuiteViewContractSuiteTag(): String {
        return PageTagEnum.contract_order_sign_success.name.uppercase()
    }

    override fun onContractSuiteBindingContractSuiteContentContractSuiteView(): View {
        mActivityBinding = ActivityContractSuiteContractSignSuccessBinding.inflate(layoutInflater)
        return mActivityBinding.root
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onContractSuiteContentContractSuiteViewContractSuiteBinded() {
        try {
            // 初始化
            // 获取用户状态回调
            mContractViewModel.onGetContractOrderListLiveData.observe(this) {
                if (it.isSuccess) {
                    it.getOrNull()?.let { result ->
                        mContractViewModel.mContractOrderList.clear()
                        mContractViewModel.mContractOrderList.addAll(result)
                    }
                }
            }

            // 数据声明下划线
            mActivityBinding.textContractDataSafety.addContractSuiteUnderContractSuiteLine()

            // 点击数据声明
            mActivityBinding.llContractDataSafety.setOnClickListener {
                // 打点
                AnalyticsUtil.saveHitPointInfo(
                    type = ContractEvent.DATA_SAFETY,
                    remark = ""
                )

                // 去数据声明页
                startContractSuiteToContractSuiteContractContractSuiteDataContractSuiteSafetyContractSuitePage()
            }

            // 如果是app来源，直接返回
            mActivityBinding.textBackToIndexSubmit.setOnClickListener {
                // 打点
                AnalyticsUtil.saveHitPointInfo(
                    type = ContractEvent.CONTRACT_BACK_TO_APP,
                    remark = ""
                )

                // 回跳
                val url =
                    "${ContractParamsFromApp.mScheme}://${ContractParamsFromApp.mHost}?isdone=true"
                val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
                startActivity(intent)
            }

            // 点击返回，直接关闭app
            mActivityBinding.textOkSubmit.setOnClickListener {
                // 打点
                AnalyticsUtil.saveHitPointInfo(
                    type = ContractEvent.CONTRACT_BACK_TO_APP,
                    remark = ""
                )

                // 回跳
                finishAffinity()
            }

            // 更新显示文案
            mActivityBinding.textContractSignSuccessTips.text = getString(
                R.string.contract_suite_contract_back_to_product,
                ContractSignManager.formatProduct()
            )

            // 判断是否为app跳转过来
            if (ContractParamsFromApp.isValidParams()) {
                mActivityBinding.textOkSubmit.visibility = View.GONE
                mActivityBinding.textBackToIndexSubmit.visibility = View.VISIBLE
                mActivityBinding.textBackToIndexSubmit.text = getString(
                    R.string.contract_suite_contract_back_to_app,
                    ContractParamsFromApp.mSourceType
                )
            } else {
                mActivityBinding.textBackToIndexSubmit.visibility = View.GONE
                mActivityBinding.textOkSubmit.visibility = View.VISIBLE
            }

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    private fun exitApp() {
        finishAffinity()
    }
}